import { CACHE_USERINFO, CACHE_TOKEN } from '../../config.js';
import Util from '../../utils/util.js';
import { userPhone } from '../../api/user.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

let app = getApp();

Component({
  properties: {
    isHidden: {
      type: Boolean,
      value: true,
    }
  },
  data: {
    cloneIner: null,
    loading: false,
  },
  attached() {
  },
  methods: {
    /** 关闭 */
    close() {
      let pages = getCurrentPages();
      let currPage = pages[pages.length - 1];
      this.setData({
        isHidden: true
      });
      if (currPage && currPage.data.isHidden != undefined) {
        currPage.setData({ isHidden: true });
      }
    },
    /** 获取并绑定手机号 */
    getPhoneNumber(e) {
      console.log(e);
      var msg = e.detail.errMsg, that = this;
      var that = this;
      var sessionKey = wx.getStorageSync('cache_key'),
        encryptedDataStr = e.detail.encryptedData,
        iv = e.detail.iv;
      if (msg == 'getPhoneNumber:ok') {
        wx.checkSession({
          success: function () {
            that.deciyption(sessionKey, encryptedDataStr, iv);
          },
          fail: function () {
            Util.getCodeLogin((res) => {
              console.log(res, 'sessionkey过期')
              that.deciyption('', encryptedDataStr, iv, res.code);
            }).catch(res => {
              wx.hideLoading();
            });
          }
        })
      }
    },
    /** 解密获取手机号信息 */
    deciyption(sessionKey, encryptedDataStr, iv, code) {
      let that = this;
      var args = {
        code: code || '',
        sessionKey: sessionKey,
        encryptedData: encryptedDataStr,
        iv: iv
      }
      userPhone(args).then(res => {
        console.log(res);
        //取消登录提示
        wx.hideLoading();
        //关闭登录弹出窗口
        that.setData({ isHidden: true, errorSum: 0 });
        //执行登录完成回调
        that.triggerEvent('onLoadFun', res);
      }).catch((err) => {
        wx.hideLoading();
        console.log(err)
        // Util.Tips({ title: err });
        //Notify({ type: 'warning', message: err });
        Toast.fail(err);
        //Toast.fail(err.sub_msg);
      });
    }
  }
})